from django.shortcuts import render
from rest_framework.generics import RetrieveUpdateAPIView,UpdateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.users.models import User
from .serializer import InfoModelSerializer,PasswordModelSerializer

"""
 /user/ 1、获取个人详情GET
        2、修改个人信息PUT
/user/password/ 3、修改密码PUT
/users/like/{id}/ 4、关注POST
                5、取消关注DELETE
"""
class UserRetrieveUpdateAPIView(RetrieveUpdateAPIView):

    permission_classes = [IsAuthenticated]

    serializer_class = InfoModelSerializer

    def get_object(self):
        return self.request.user

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        dict_data = serializer.data
        dict_data["answer_question"] = dict_data.pop("replies")
        return Response(dict_data)
class PasswordUpdateAPIView(UpdateAPIView):

    permission_classes = [IsAuthenticated]

    serializer_class = PasswordModelSerializer

    def get_object(self):
        return self.request.user
class LikeAPIView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self,request,id):
        user = request.user
        idol = User.objects.get(id = id)
        user.idols.add(idol)
        return Response({"success":True,"message":"关注成功"})
    def delete(self,request,id):
        user = request.user
        user.idols.clear()
        return Response({"success":True,"message":"取消关注成功"})

